package com.music.mysongs.receiver

import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Process
import com.music.mysongs.helper.myClear
import com.music.mysongs.helper.myCxtReadIntSPF
import com.music.mysongs.helper.myCxtWriteSPF
import com.music.mysongs.utils.ActivityCollector
import com.music.mysongs.utils.myLog
import com.music.mysongs.utils.myToastWithoutCxt


class OffLineReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            "com.music.mysongs.OFFLINE" -> {
                AlertDialog.Builder(context).apply {
                    setTitle("朋友啊再见吧再见吧再见叭 886")
                    setMessage("记录的数据会被清空哦!")
                    setCancelable(false)
                    setNegativeButton("留下来玩~") { _, _ ->
                        myToastWithoutCxt("呐！听歌去")
                    }
                    setPositiveButton("拜呀~") { _, _ ->
                        context.getSharedPreferences("DataSPF", Context.MODE_PRIVATE).myClear()
                        ActivityCollector.finishAll()
                    }
                    show()
                }
            }
            "com.music.mysongs.THEME" -> {
                val themeColor = arrayOf("浅蓝", "淡蓝", "清蓝", "湖蓝", "五彩斑斓的黑")
                var chk = myCxtReadIntSPF(context, "spfTheme")
                AlertDialog.Builder(context).apply {
                    setTitle("更改主题色(重启后生效哦| >.< )")
                    setCancelable(true)
                    setSingleChoiceItems(
                        themeColor, chk!!
                    ) { _, i ->
                        chk = i
                        myLog("chk=$chk i=$i")
                    }
                    setPositiveButton("保存并重启") { _, _ ->
                        myCxtWriteSPF(context, "spfTheme", null, chk)
                        myToastWithoutCxt("正在换色..")
                        val launchIntent =
                            context.packageManager.getLaunchIntentForPackage(context.packageName)
                        launchIntent!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                        context.startActivity(launchIntent)
                        Process.killProcess(Process.myPid())
                    }
                    show()
                }
            }
        }
    }
}